﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace WBFSe3.IO
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public class WbfsIOException : Exception
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        [DllImport("Kernel32.dll", SetLastError = true)]
        private static extern uint FormatMessage(uint dwFlags, IntPtr lpSource,
            uint dwMessageId, uint dwLanguageId, ref IntPtr lpBuffer,
            uint nSize, IntPtr pArguments);


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr LocalFree(IntPtr hMem);


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private int nativeError;
        private string message;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public int NativeError { get { return this.nativeError; } }
        public override string Message { get { return this.message; } }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsIOException()
        {
            IntPtr pMessage = IntPtr.Zero;
            uint dwChars;

            this.nativeError = Marshal.
                GetLastWin32Error();

            dwChars = FormatMessage(0x00000100 | 0x00001000 | 0x00000200, IntPtr.Zero, 
                (uint)this.nativeError, 0, ref pMessage, 0, IntPtr.Zero);

            if (dwChars == 0)
                this.message = "No message";

            this.message = Marshal.PtrToStringAnsi(pMessage);
            LocalFree(pMessage);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsIOException(int error)
        {
            IntPtr pMessage = IntPtr.Zero;
            uint dwChars;

            this.nativeError = error;

            dwChars = FormatMessage(0x00000100 | 0x00001000 | 0x00000200, IntPtr.Zero,
                (uint)this.nativeError, 0, ref pMessage, 0, IntPtr.Zero);

            if (dwChars == 0)
                this.message = "No message";

            this.message = Marshal.PtrToStringAnsi(pMessage);
            LocalFree(pMessage);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsIOException(string message)
            : base(message)
        {
            this.nativeError = Marshal.
                GetLastWin32Error();
            this.message = message;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsIOException(string message, int error)
            : base(message)
        {
            this.nativeError = error;
            this.message = message;
        }
    }
}